package top.kaoshanji.p1ch04b;

import java.text.MessageFormat;
import java.util.ListResourceBundle;
import java.util.MissingResourceException;
import java.util.ResourceBundle;

/**
 * 从资料包中读取消息文本的辅助Java类
 * author kaoshanji
 * ^_^
 * create time 2025/1/16 19:07
 */
public class Messages {

    private static ResourceBundle bundle;

    static {
        try {
            bundle = ResourceBundle.getBundle("p1ch04b01/Messages", LocaleHolder.get());
        } catch (MissingResourceException e) {
            bundle = new ListResourceBundle() {
                @Override
                protected Object[][] getContents() {
                    return new Object[0][];
                }
            };
        }
    }

    public static String get(String key, Object... args) {
        try {
            String value = bundle.getString(key);
            return MessageFormat.format(value, args);
        } catch (MissingResourceException e) {
            return "!!!" + key;
        }
    }

}
